package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/food")
@RestController
public class FoodController1 {

    @Reference
    private FoodService foodService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){

        //读取Excel文件数据
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list!=null&&list.size()>0){
                List<Food> foodList=new ArrayList<>();
                for (String[] strings : list) {
                    String foodName = strings[0];
                    String foodExplain = strings[1];
                    String foodSize = strings[2];
                    String age = strings[3];
                    Food food = new Food(foodName,foodExplain,foodSize,age);
                    foodList.add(food);
                }
                foodService.add(foodList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_FOOD_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_FOOD_SUCCESS);

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Food food){
        try {
            foodService.addFood(food);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, "膳食添加失败");
        }
        return new Result(true,"膳食添加成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer foodId){
        try {
            Food food=foodService.findById(foodId);
            return new Result(true,"查询膳食成功",food);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false,"查询膳食失败");
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult result = foodService.pageQuery(queryPageBean);
        return result;
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Food food){
        try {
            foodService.edit(food);
        }catch(Exception ex){
            ex.printStackTrace();
            return new Result(false,"膳食编辑失败");
        }

        return new Result(true,"膳食编辑成功");
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer foodId){
        try {
            foodService.deleteById(foodId);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, "膳食删除失败");
        }
        return new Result(true,"膳食删除成功");
    }

}
